package cqupt.spark_log.info;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cqupt.spark_log.entity.EnergyTime;
import cqupt.spark_log.fileio.ExcelMan;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExcelInfo {
	private Map<String, WritableWorkbook> bookMap = new HashMap<String, WritableWorkbook>();

	public void saveInfo(String algorithm, String parallel, String index, String workLoad,String name) {
		try {
			WritableWorkbook book = this.getBook(workLoad);
			if (book == null) {
				book = Workbook.createWorkbook(new File("excel/" + workLoad + ".xls"));
				this.setBook(workLoad, book);
			}

			WritableSheet sheet = null;
			String[] sheetNames = book.getSheetNames();
			if (isInclue(sheetNames, parallel)) {
				sheet = book.getSheet(parallel);
			} else {
				sheet = book.createSheet(parallel, Integer.parseInt(parallel));
				initSheet(sheet);
			}
			ExcelMan excelman = new ExcelMan();
			EnergyTime energyTime = excelman.getresult(workLoad,name);
			//得到行的值
			int row = Integer.parseInt(index)+1;//因为索引从1开始的，而jxl是从0开始的
			//得到列的值
			int clo = 0;
			if ("FIFO".equals(algorithm)) {
				clo = 1;
			} else if ("FAIR_FAIR".equals(algorithm)) {
				clo = 3;
			} else if ("ENERGY_AWARE".equals(algorithm)) {
				clo = 5;
			} else if ("ENERGY_AWARE_0.0.2".equals(algorithm)) {
				clo = 7;
			} 
//			else if ("ENERGY_AWARE_0.0.2".equals(algorithm)) {
//				clo = 9;
//			}
			//写入数据
			Label labelindex = new Label(0, row, index);
			Number labelenergy = new Number(clo, row, Double.parseDouble(energyTime.getEnergy()));
			Number labeltime = new Number(clo+1, row, Integer.parseInt(energyTime.getTime()));
			sheet.addCell(labelindex);
			sheet.addCell(labelenergy);
			sheet.addCell(labeltime);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}

	private void initSheet(WritableSheet sheet) {
		try {
			Label labelFIFO = new Label(1, 0, "FIFO");
			Label labelFAIR = new Label(3, 0, "FAIR");
			Label labelENERGY = new Label(5, 0, "ENERGY");
			Label labelEXE = new Label(7, 0, "ENERGY_0.0.2");
			Label labelenergy1 = new Label(1, 1, "energy");
			Label labelruntime1 = new Label(2, 1, "runtime");
			Label labelenergy2 = new Label(3, 1, "energy");
			Label labelruntime2 = new Label(4, 1, "runtime");
			Label labelenergy3 = new Label(5, 1, "energy");
			Label labelruntime3 = new Label(6, 1, "runtime");
			Label labelenergy4 = new Label(7, 1, "energy");
			Label labelruntime4 = new Label(8, 1, "runtime");
			sheet.addCell(labelFIFO);
			sheet.addCell(labelFAIR);
			sheet.addCell(labelENERGY);
			sheet.addCell(labelEXE);
			sheet.addCell(labelenergy1);
			sheet.addCell(labelruntime1);
			sheet.addCell(labelenergy2);
			sheet.addCell(labelruntime2);
			sheet.addCell(labelenergy3);
			sheet.addCell(labelruntime3);
			sheet.addCell(labelenergy4);
			sheet.addCell(labelruntime4);
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}

	private boolean isInclue(String[] sheetNames, String parallel) {
		for (int i = 0; i < sheetNames.length; i++) {
			if (parallel.equals(sheetNames[i])) {
				return true;
			}
		}
		return false;
	}

	public WritableWorkbook getBook(String key) {
		return bookMap.get(key);
	}

	public void setBook(String key, WritableWorkbook workbook) {
		bookMap.put(key, workbook);
	}

	public void writeAndclose() {
		try {
			for (String key : bookMap.keySet()) {
				WritableWorkbook book = bookMap.get(key);
				book.write();
				book.close();
			}
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
